package com.cs.statistic.utiltool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import java.lang.reflect.Method;

public class DrawUtils {
    public static float sDensity = 1.0F;
    public static int sDensityDpi;
    public static float sFontDensity;
    public static int sWidthPixels = -1;
    public static int sHeightPixels = -1;
    public static int sRealWidthPixels = -1;
    public static int sRealHeightPixels = -1;
    private static Class<?> sClass = null;
    private static Method sMethodForWidth = null;
    private static Method sMethodForHeight = null;
    public static float sVirtualDensity = -1.0F;
    public static float sVirtualDensityDpi = -1.0F;
    public static int sTouchSlop;

    public static int dip2px(float dipVlue) {
        return (int) (dipVlue * sDensity + 0.5F);
    }

    public static int px2dip(float pxValue) {
        float scale = sDensity;
        return (int) (pxValue / scale + 0.5F);
    }

    public static int sp2px(float spValue) {
        float scale = sDensity;
        return (int) (scale * spValue);
    }

    public static int px2sp(float pxValue) {
        float scale = sDensity;
        return (int) (pxValue / scale);
    }

    @SuppressLint({"NewApi"})
    public static synchronized void resetDensity(Context context) {
        if ((context != null) && (null != context.getResources())) {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            sDensity = metrics.density;
            sFontDensity = metrics.scaledDensity;
            sDensityDpi = metrics.densityDpi;

            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            try {
                Display display = wm.getDefaultDisplay();
                sWidthPixels = display.getWidth();
                sHeightPixels = display.getHeight();
                Class<?> clazz = Class.forName("android.view.Display");
                Point realSize = new Point();
                Method method = clazz.getMethod("getRealSize", new Class[]{Point.class});
                method.invoke(display, new Object[]{realSize});
                sRealWidthPixels = realSize.x;
                sRealHeightPixels = realSize.y;
            } catch (Throwable e) {
                e.printStackTrace();
                sRealWidthPixels = sWidthPixels;
                sRealHeightPixels = sHeightPixels;
            }
            try {
                ViewConfiguration configuration = ViewConfiguration.get(context);
                if (null != configuration) {
                    sTouchSlop = configuration.getScaledTouchSlop();
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    public static int getTabletScreenWidth(Context context) {
        int width = 0;
        if (context != null) {
            try {
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                Display display = wm.getDefaultDisplay();
                if (sClass == null) {
                    sClass = Class.forName("android.view.Display");
                }
                if (sMethodForWidth == null) {
                    sMethodForWidth = sClass.getMethod("getRealWidth", new Class[0]);
                }
                width = ((Integer) sMethodForWidth.invoke(display, new Object[0])).intValue();
            } catch (Exception localException) {
            }
        }
        if (width == 0) {
            width = getRealWidth(context);
        }
        return width;
    }

    public static int getTabletScreenHeight(Context context) {
        int height = 0;
        if (context != null) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            try {
                if (sClass == null) {
                    sClass = Class.forName("android.view.Display");
                }
                if (sMethodForHeight == null) {
                    sMethodForHeight = sClass.getMethod("getRealHeight", new Class[0]);
                }
                height = ((Integer) sMethodForHeight.invoke(display, new Object[0])).intValue();
            } catch (Exception localException) {
            }
        }
        if (height == 0) {
            height = getRealHeight(context);
        }
        return height;
    }

    public static void setVirtualDensity(float density) {
        sVirtualDensity = density;
    }

    public static void setVirtualDensityDpi(float densityDpi) {
        sVirtualDensityDpi = densityDpi;
    }

    public static int getRealWidth(Context context) {
        if ((sRealWidthPixels == -1) || (sWidthPixels == -1)) {
            resetDensity(context);
        }
        return sWidthPixels;
    }

    public static int getRealHeight(Context context) {
        if ((sRealHeightPixels == -1) || (sHeightPixels == -1)) {
            resetDensity(context);
        }
        return sHeightPixels;
    }

    public static double getFontHeight(float fontSize) {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.top) + dip2px(1.0F);
    }

    public static int getScreenWidth(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return metrics.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }

    public static int getScreenDPI(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return metrics.densityDpi;
    }

    public static boolean isLandScreen(Context context) {
        if (getScreenWidth(context) > getScreenHeight(context)) {
            return true;
        }
        return false;
    }
}
